/**
 * Created by Jellyleo on 2019年12月16日
 * Copyright © 2019 jellyleo.com
 * All rights reserved.
 */
package com.itcast.activiti.controller;

import cn.itcast.ic.common.bean.Response;
import com.itcast.activiti.service.IHistoryService;
import com.itcast.activiti.utils.ActivitiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 历史控制器
 */
@RestController
@RequestMapping("/history")
public class HistoricController {
    @Autowired
    private IHistoryService iHistoryService;

    /**
     * 获取历史流程实例
     *
     * @return
     */
    @PostMapping(value = "/process")
    public Response listHistoricProcessInstancePage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(iHistoryService.listHistoricProcessInstancePage(activitiPage));
    }

    /**
     * 获取历史流程执行任务记录
     *
     * @return
     */
    @PostMapping(value = "/task")
    public Response listHistoricTaskInstancePage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(iHistoryService.listHistoricTaskInstancePage(activitiPage));
    }

    /**
     * 查询历史流程步骤
     *
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(value = "/activity/{businessKey}")
    public Response<List<Map<String, Object>>> listHistoricActivityInstance(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String tenantId) {
        return Response.success(iHistoryService.listHistoricActivityInstanceByBusinessKey(businessKey, StringUtils.hasText(tenantId) ? tenantId : "ITCAST_PMIS"));
    }

    @GetMapping(value = "/activity/{businessKey}/desc")
    public Map<String, Object> listHistoricActivityInstanceByBusinessKeyDesc(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String tenantId) {
        return iHistoryService.listHistoricActivityInstanceByBusinessKeyDesc(businessKey, StringUtils.hasText(tenantId) ? tenantId : "ITCAST_PMIS");
    }
}
